﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DOMercury.Interfaces;

namespace Bezel__
{
    /// <summary>
    /// Interaction logic for SelectedElementDisplayer.xaml
    /// </summary>
    public partial class SelectedElementDisplayer : UserControl
    {
        private bool active = false;
        private bool foundItems = false;
        private IItem currentItem;
        private IAction currentAction;
        private Color ActiveColor, InactiveColor;
        private SolidColorBrush ActiveBrush, InactiveBrush;

        
        public SelectedElementDisplayer()
        {
            InitializeComponent();
            lbSelectedItems.Opacity = 0;
            lbSelectedItems.Items.Clear();
            InactiveColor = new Color(); //BF002661
            ActiveColor = new Color(); //076EFF
            InactiveColor.A = 0xBF;
            InactiveColor.R = 0x00;
            InactiveColor.G = 0x26;
            InactiveColor.B = 0x61;
            
            ActiveColor.A = 0xCF;
            ActiveColor.R = 0x07;
            ActiveColor.G = 0x6E;
            ActiveColor.B = 0xFF;
            ActiveBrush = new SolidColorBrush(ActiveColor);
            InactiveBrush = new SolidColorBrush(InactiveColor);
            this.ClearDisplayer();
        }


        #region Properties

        public bool FoundItems
        {
            get
            {
                return foundItems;
            }
            set
            {
                foundItems = value;
            }
        }

        public IItem CurrentItem
        {
            get
            {
                return currentItem;
            }
            set
            {
                currentItem = value;
                CurrentItemChanged();
            }
        }

        public IAction CurrentAction
        {
            get
            {
                return currentAction;
            }
            set
            {
                currentAction = value;
                CurrentActionChanged();
            }
        }

        public bool Active
        {
            get
            {
                return active;
            }
            set
            {
                active = value;
                ActiveStatusChanged();
            }
        }
        #endregion

        private void ActiveStatusChanged()
        {
            if (active)
            {
                ItemPane.Background = ActiveBrush;
            }
            else
            {
                ItemPane.Background = InactiveBrush;
            }
        }

        private void CurrentItemChanged()
        {
            if (foundItems)
            {
                SetItemsListMode();
            }
            else
            {
                SetNormalMode();
            }
            
        }

        private void SetItemsListMode()
        {
            try
            {
                imgItem.Opacity = 0;
                imgItemReflection.Opacity = 0;
                
                lbSelectedItems.ItemsSource = ((DOMercury.Items.ItemsList)(currentItem)).Items;
                lbSelectedItems.SelectedIndex = -1;
                lbSelectedItems.Opacity = 1;
                Console.WriteLine(((DOMercury.Items.ItemsList)(currentItem)).Items.Count);
                CurrentItemName.Content = currentItem.DisplayName;
            }
            catch
            {
                SetNormalMode();
            }
        }

        private void SetNormalMode()
        {
            lbSelectedItems.Opacity = 0;
            if (imgItem.Opacity == 0)
            {
                imgItem.Opacity = 1;
                imgItemReflection.Opacity = 1;
            }
            BitmapSource icon = getIcon(currentItem);
            imgItem.Source = icon;
            imgItemReflection.Source = icon;
            CurrentItemName.Content = currentItem.DisplayName;
        }

        private void CurrentActionChanged()
        {
            BitmapSource icon = getIcon(currentAction);
            imgItem.Source = icon;
            imgItemReflection.Source = icon;
            CurrentItemName.Content = currentAction.ActionType;
        }

        public void ClearDisplayer()
        {
            lbSelectedItems.ItemsSource = null;
            imgItem.Source = null;
            imgItemReflection.Source = null;
            CurrentItemName.Content = "";
        }


        private BitmapSource getIcon(IItem item)
        {
            return getIcon(item.ItemType);
        }

        private BitmapSource getIcon(IAction action)
        {
            return getIcon(action.ActionType);
        }

        private BitmapSource getIcon(string type)
        {
            return DOMercury.Engine.Interface.EngineCore.GetIconImageWPF(type);
        }

    }
}
